<?php

namespace Largezhou\Admin\Commands;

use Illuminate\Routing\Console\ControllerMakeCommand;

class MakeController extends ControllerMakeCommand
{
    protected $name = 'admin:make-controller';

    protected $description = '创建一个后台控制器模板';

    protected function getStub()
    {
        return __DIR__.'/stubs/controller.stub';
    }

    protected function buildClass($name)
    {
        $controllerNamespace = $this->getNamespace($name);

        $replace = [];

        $replace = $this->buildModelReplacements($replace);

        $replace["use {$controllerNamespace}\Controller;\n"] = '';

        return str_replace(
            array_keys($replace), array_values($replace), parent::buildClass($name)
        );
    }

    protected function buildModelReplacements(array $replace)
    {
        $modelClass = $this->parseModel($this->ask('输入model路径'));

        return array_merge($replace, [
            'DummyFullModelClass' => $modelClass,
            'DummyModelClass'     => class_basename($modelClass),
            'DummyModelVariable'  => lcfirst(class_basename($modelClass)),
        ]);
    }
}
